<?php
 
// DB TABLE Exporter
//
// How to use:
//
// Place this file in a safe place, edit the info just below here
// browse to the file, enjoy!
 
// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO
 
   //  $dbhost  = "127.0.0.1:3306";
   //  $dbuser  = "XXXXX";
   //  $dbpass  = "XXXXX";
   //  $dbname  = "XXXXX";
   //  $dbtable = "XXXXX";
 
// END CHANGING STUFF
 
 
// first thing that we are going to do is make some functions for writing out
// and excel file. These functions do some hex writing and to be honest I got 
// them from some where else but hey it works so I am not going to question it 
// just reuse
 
 
// This one makes the beginning of the xls file
function xlsBOF() {
    //	echo mb_convert_encoding(pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0), 'UTF-16LE', 'UTF-8'); 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}
 
// This one makes the end of the xls file
function xlsEOF() {
    //	echo mb_convert_encoding(pack("ss", 0x0A, 0x00), 'UTF-16LE', 'UTF-8'); 
		
    echo pack("ss", 0x0A, 0x00);
    return;
}
 
// this will write text in the cell you specify
function xlsWriteLabel($Row, $Col, $Value ) {
	//$Value=mb_convert_encoding($Value, 'UTF-16LE', 'UTF-8'); 
    $L = strlen($Value);
	//echo mb_convert_encoding(pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L), 'UTF-16LE', 'UTF-8'); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
    return;
}
 
 $reports=getReportName();
 
// make the connection an DB query
//$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
//print_r($_POST);
$sql="";
if(!isset($_POST['option'])){

}elseif(!array_key_exists($_POST['option'], $reports)){
	
}


if(isset($_POST['targetYear'])){
	$option=$_POST['option'];
	$targetYear=$_POST['targetYear'];
	$targetMonth=$_POST['targetMonth'];
	$sql="CALL `$option` ('$targetYear','$targetMonth')";
}elseif(isset($_POST['fromYear'])){
	$option=$_POST['option'];
	$fromYear=$_POST['fromYear'];
	$fromMonth=$_POST['fromMonth'];
	$fromDate=$_POST['fromDate'];
	$toMonth=$_POST['toMonth'];
	$toYear=$_POST['toYear'];
	$toDate=$_POST['toDate'];
	$sql="CALL `$option` (
 	'$fromYear-$fromMonth-$fromDate',  '$toYear-$toMonth-$toDate'
	)";
}else{
	//error
}


		$qr = mysql_query( $sql ) or die( mysql_error() );
		$name="wahaha";
		
		
		
		
// Ok now we are going to send some headers so that this 
// thing that we are going make comes out of browser
// as an xls file.
// 
header("Pragma: public");
header("Content-type: application/x-msexcel"); 
header('Content-Disposition: attachment;  filename="utf8_bom.xls"');
//header("Expires: 0");
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("Content-Type: application/octet-stream");
//header('Content-Encoding: UTF-8');
//header('Content-type: text/xls; charset=UTF-8');
//this line is important its makes the file name
//header("Content-Disposition: attachment;filename=export_".$name.".xls ");
 
//header("Content-Transfer-Encoding: binary ");
  //echo "\xFF\xFE"; // UTF-8 BOM
// start the file
echo "\xEF\xBB\xBF";
xlsBOF();

// these will be used for keeping things in order.
$col = 0;
$row = 0;
 
// This tells us that we are on the first row
$first = true;
$content="";
while( $qrow = mysql_fetch_assoc( $qr ) )
{
    // Ok we are on the first row
    // lets make some headers of sorts
    if( $first )
    {
        foreach( $qrow as $k => $v )
        {
    //  $k = mb_convert_encoding($k, 'UTF-16LE', 'UTF-8'); 
			
            // take the key and make label
            // make it uppper case and replace _ with ' '
            //xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
			            xlsWriteLabel( $row, $col,  $k  );
            $col++;
        }
 
        // prepare for the first real data row
        $col = 0;
        $row++;
        $first = false;
    }
 
    // go through the data
    foreach( $qrow as $k => $v )
    {
        // write it out
        xlsWriteLabel( $row, $col, $v );
        $col++;
    }
    // reset col and goto next row
    $col = 0;
    $row++;
}
 
xlsEOF();
exit();
?>